const {http}=require("../../utils/http");
const {cartDelete,orderInsert
}=require("../../utils/insertAndDelete");
Page({
  data:{
    currentCity:"",
    cart:[],
    totalPrice:0,
    totalChoose:"X"
  },
  onLoad:function(option){
    var url="http://122.112.245.197:5000/api/cart";
    http({
      url,
      success:(res)=>{
        // console.log(res.data)
        var cart=[];
        res.data.forEach(item=>{
          if(!wx.getStorageSync('iphone')){
            return false;
          }
          else if(item.userID==wx.getStorageSync('iphone')){
            cart.push(item);
          } 
        })
        this.setData({
          cart:cart,
        })
      }
    })
    var totalPrice=0;
    this.data.cart.forEach(item=>{
      if(item.choose=="√"){
        totalPrice+=item.num*item.price*100;
      }
    })
    this.setData({
      totalPrice:totalPrice
    })

    
  },
  choose(e){
    console.log(e)
    if(this.data.cart[e.currentTarget.dataset.aid].choose=="X"){
      var cart=this.data.cart;
      cart[e.currentTarget.dataset.aid].choose="√"
      this.setData({
        cart:cart
      })
      var totalPrice=this.data.totalPrice+this.data.cart[e.currentTarget.dataset.aid].price*100;
      this.setData({
        totalPrice:totalPrice
      })
    }
    else{
      var cart=this.data.cart;
      cart[e.currentTarget.dataset.aid].choose="X"
      this.setData({
        cart:cart
      })
      var totalPrice=this.data.totalPrice-this.data.cart[e.currentTarget.dataset.aid].price*100;
      this.setData({
        totalPrice:totalPrice
      })
    }
  },
  delete(e){
    var data={
      pID:this.data.cart[e.currentTarget.dataset.aid].pID
    }
    cartDelete({
      data,
      success:(res)=>{
        var cart=this.data.cart;
        var totalPrice=this.data.totalPrice;
        if(cart[e.currentTarget.dataset.aid]=="√") {
          var totalPrice=this.data.totalPrice-this.data.cart[e.currentTarget.dataset.aid].price*100;
        }
        cart.splice(e.currentTarget.dataset.aid,1);
        wx.showToast({
          title: '删除成功！',
          });
        this.setData({
          cart:cart,
          totalPrice:totalPrice
        });
        }
      
    })
  },
  totalChoose(){
    var totalChoose=this.data.totalChoose;
    var cart=this.data.cart;
    var totalPrice=0;
    if(totalChoose=="X"){
      cart.forEach(item=>{
        item.choose="√";
        totalPrice+=item.num*item.price*100;
      })
      totalChoose="√";
      this.setData({
        totalPrice:totalPrice,
        totalChoose:totalChoose,
        cart:cart
      })
    }
    else{
      cart.forEach(item=>{
        item.choose="X";
      })
      totalChoose="X";
      this.setData({
        totalPrice:totalPrice,
        totalChoose:totalChoose,
        cart:cart
      })
    }
  },
  jian(e){
    if(this.data.cart[e.currentTarget.dataset.aid].num==1){
      return false;
    }
    else{
      var cart=this.data.cart;
      var totalPrice=this.data.totalPrice;
      cart[e.currentTarget.dataset.aid].num--;
      totalPrice-=cart[e.currentTarget.dataset.aid].price*100
      if(totalPrice<0){
        totalPrice=0;
      }
      this.setData({
        cart:cart,
        totalPrice:totalPrice
      })
    }
  },
  add(e){
    var cart=this.data.cart;
    var totalPrice=this.data.totalPrice;
    cart[e.currentTarget.dataset.aid].num++;
    if(cart[e.currentTarget.dataset.aid].choose=="√"){
      totalPrice+=cart[e.currentTarget.dataset.aid].price*100
    }
    this.setData({
        cart:cart,
        totalPrice:totalPrice
      })
  },
  onClickButton(){
    var cart=this.data.cart;
    var totalPrice=this.data.totalPrice;
    var data={
      address:wx.getStorageSync('city'),
      userID:wx.getStorageSync('iphone'),
      cart:cart,
      totalPrice:totalPrice
    }
    orderInsert({
      data,
      success:(res)=>{
        wx.showToast({
          title: '提交成功！',
          })

          //在此处添加代码
      }
    })
    wx.navigateTo({
      url: '../../pages/order/index',
    })
  },
  onShow(){
    var city = wx.getStorageSync("city");
     this.setData({
      currentCity:city
    })
    this.onLoad();
  },
  handleClick(){
    
    wx-wx.navigateTo({
      url: '/pages/address/address'
    });
  },
})